home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / MPEG / AMPBitstream.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  4.0 KB  |  184 lines

  1. /* 
  2.  *  AmpBitstream.cpp
  3.  *
  4.  *  Code from
  5.  *            NekoAmp 1.3 decoder by Avery Lee
  6.  *
  7.  *  FlasKMPEG
  8.  *    Copyright (C) Alberto Vigata - January 2000
  9.  *
  10.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  11.  *    
  12.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2, or (at your option)
  15.  *  any later version.
  16.  *   
  17.  *  FlasKMPEG is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *   
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with GNU Make; see the file COPYING.  If not, write to
  24.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  25.  *
  26.  */
  27.  
  28. #include <crtdbg.h>
  29.  
  30. #include "AMPBitstream.h"
  31.  
  32. void AMPBitstream::resetbits(int bytes) {
  33.     bufindex = 0;
  34.     bitcnt = 24;
  35.     bitheap = 0;
  36.  
  37.     if (bytes) {
  38.         bufindexw = bufpoint = pSource->read(buf, bytes);
  39.  
  40.         if (bufpoint != bytes)
  41.             throw (int)IAMPDecoder::ERR_EOF;
  42.     } else
  43.         bufindexw = bufpoint = 0;
  44. }
  45.  
  46. void AMPBitstream::fillbits(int bytes) {
  47.     int actual;
  48.  
  49.     if (bufindexw + bytes > BUFFER_SIZE) {
  50.         actual = pSource->read(buf + bufindexw, BUFFER_SIZE - bufindexw);
  51.         if (actual != BUFFER_SIZE-bufindexw)
  52.             throw (int)IAMPDecoder::ERR_EOF;
  53.  
  54.         bufindexw = bufindexw+bytes-BUFFER_SIZE;
  55.  
  56.         actual = pSource->read(buf, bufindexw);
  57.  
  58.         if (actual != bufindexw)
  59.             throw (int)IAMPDecoder::ERR_EOF;
  60.  
  61.     } else {
  62.         actual = pSource->read(buf + bufindexw, bytes);
  63.  
  64.         if (actual != bytes)
  65.             throw (int)IAMPDecoder::ERR_EOF;
  66.  
  67.         bufindexw += actual;
  68.         if (bufindexw >= BUFFER_SIZE)
  69.             bufindexw -= BUFFER_SIZE;
  70.     }
  71.  
  72.     bufpoint += bytes;
  73.     if (bufpoint > BUFFER_SIZE)
  74.         bufpoint = BUFFER_SIZE;
  75. }
  76.  
  77. void AMPBitstream::rewind(int bytes) {
  78.     // restore bytes in buffer
  79.  
  80.     if (bitcnt <= 16)
  81.         bufpoint += (24-bitcnt)>>3;
  82. //        bufpoint += (24+7-bitcnt)>>3;
  83.  
  84. //    if (bufpoint < bytes)
  85. //        _RPT2(0,"\t\t\t------Not enough bytes! needed=%d, actual=%d\n", bytes, bufpoint);
  86.  
  87.     if (bufpoint < bytes)
  88.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  89.  
  90.     bitcnt = 24;
  91.     bitheap = 0;
  92.     bufindex = (bufindexw + BUFFER_SIZE - bytes) & BUFFER_MASK;
  93.     bufpoint = bytes;
  94. }
  95.  
  96. void AMPBitstream::rewindbits(int bits) {
  97.     long actualbits = tellbits();
  98.  
  99.     // allow up to 24 bits of oopsie
  100.  
  101.     if (bits > actualbits+24)
  102.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  103.  
  104.     if (bits == actualbits)
  105.         return;
  106.  
  107.     if (actualbits > bits && actualbits - bits <= 16)
  108.         getbits(actualbits - bits);
  109.     else {
  110.  
  111.         bitcnt = 24;
  112.         bitheap = 0;
  113.         bufpoint = ((bits+7)>>3);
  114.         bufindex = (bufindexw + BUFFER_SIZE - bufpoint) & BUFFER_MASK;
  115.  
  116.         if (bits & 7)
  117.             getbits(8-(bits & 7));
  118.     }
  119. }
  120.  
  121. int AMPBitstream::tellbits() {
  122.     return bufpoint*8 + (24-bitcnt);
  123. }
  124.  
  125. long AMPBitstream::_peekbits(unsigned char bits) {
  126.     long rv;
  127.  
  128.     if (!bits)
  129.         return 0;
  130.  
  131.     while(bitcnt >= 0 && bufpoint>0) {
  132.         --bufpoint;
  133.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  134.         bufindex &= BUFFER_MASK;
  135.         bitcnt -= 8;
  136.     }
  137.  
  138.     if (bitcnt > 24-bits)
  139.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  140.  
  141.     return bitheap >> (32-bits);
  142. }
  143.  
  144. long AMPBitstream::_peekbits2(unsigned char bits) {
  145.     long rv;
  146.  
  147.     if (!bits)
  148.         return 0;
  149.  
  150.     while(bitcnt >= 0 && bufpoint>0) {
  151.         --bufpoint;
  152.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  153.         bufindex &= BUFFER_MASK;
  154.         bitcnt -= 8;
  155.     }
  156.  
  157.     return bitheap >> (32-bits);
  158. }
  159.  
  160. long AMPBitstream::_getbits(unsigned char bits) {
  161.     long rv;
  162.  
  163.     if (!bits)
  164.         return 0;
  165.  
  166.     while(bitcnt >= 0 && bufpoint>0) {
  167.         --bufpoint;
  168.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  169.         bufindex &= BUFFER_MASK;
  170.         bitcnt -= 8;
  171.     }
  172.  
  173.     if (bitcnt > 24-bits)
  174.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  175.  
  176.     rv = bitheap >> (32-bits);
  177.  
  178.     bitcnt += bits;
  179.  
  180.     bitheap <<= bits;
  181.  
  182.     return rv;
  183. }
  184.